/*
	File:		AIShip.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	Ship that moves on its own by calling the AI_Update function.
*/

#include "AIShip.h"
#include <Windows.h>
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../SGD Wrappers/CSGD_XAudio2.h"
#include "../SGD Wrappers/CSGD_MessageSystem.h"

#include "Message.h"
#include "CreateLaserMessage.h"
#include "Game.h"
#include <cassert>

#define HALVE_PI 1.57079632f

CAIShip::CAIShip() : CShip()
{
	m_eType		= ENT_AISHIP;
}

CAIShip::~CAIShip()
{
	CShip::~CShip();
}

void CAIShip::Update( float fElapsedTime )
{
	PowerUp_Update(fElapsedTime);

	if(spinOutTimer>0)
		SpinOut(fElapsedTime);
	else
		AI_Update(fElapsedTime);
	CShip::Update(fElapsedTime);
}

void rotate_point(float cx, float cy, float angle, POINT& p)
{
	float s = sin(angle);
	float c = cos(angle);

	p.x -= (LONG)cx;
	p.y -= (LONG)cy;

	float xnew = p.x * c - p.y * s;
	float ynew = p.x * s + p.y * c;

	p.x = (LONG)(xnew + cx);
	p.y = (LONG)(ynew + cy);
}

void CAIShip::Render( void )
{
	CGame* gamePtr = CGame::GetInstance();

	assert( GetImageID() != -1 && "CShip::Render - invalid image id!" );
	
	POINT start = {(int)GetPosX()-gamePtr->GetCameraX(),(int)GetPosY()-gamePtr->GetCameraY()};
	rotate_point(gamePtr->GetWidth()/2.0f,gamePtr->GetHeight()/2.0f,gamePtr->GetCameraRotation(),start);
	
	RECT srcRECT = {0,0,512,512};
	float scale = 512.0f / GetWidth();
	
	CSGD_TextureManager::GetInstance()->Draw( GetImageID(),
		start.x-GetWidth()/2,start.y-GetHeight()/2,
		1.0f/scale,1.0f/scale,&srcRECT, 
		256,256,m_fRotation+gamePtr->GetCameraRotation());

	if(CGame::DEBUG_MODE)
	{
		RECT collisonRECT = {(LONG)(start.x-GetWidth()*0.5f),(LONG)(start.y-GetHeight()*0.5f),(LONG)(start.x+GetWidth()*0.5f),(LONG)(start.y+GetHeight()*0.5f)};
		CSGD_Direct3D::GetInstance()->DrawRect(collisonRECT,255,0,0);
	}
}